package dw2;

import java.awt.*;
import java.util.Random;

public class Particle {

    protected Random random = new Random();
    protected double x = 0, y = 0, direction, speed = 2, age = 0, life = 50;
    protected double radius = 0;
    protected Color color;
    protected double gravity = .1;
    protected boolean removeOnGround;
    public Rectangle bounds;

    public Particle(double x, double y, Color color, double radius, double radius_rand) {
        bounds = new Rectangle((int)x,(int)y,(int)radius,(int)radius);
        direction = random.nextInt(90) + 225;
        speed = 2 + ((random.nextDouble() * 2) - 1);
        this.radius = radius + ((random.nextDouble() * (radius_rand * 2)) - radius_rand);
        this.x = x;
        this.y = y;
        this.color = color;
        this.removeOnGround = true;
    }

    public double calcXYspeed(double x2, double y2) {
        int x1 = 0, y1 = 0;
        double H = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); // The hypotenuse
        double x = x2 - x1; // The opposite
        double y = y2 - y1; // The adjacent
        double angle = Math.acos(x / H);
        angle = angle * 57.2960285258;
        if (y < 0) {
            angle = 360 - angle;
        }
        return angle;
    }
    public void toggleRemoveOnGround() {
        removeOnGround = !removeOnGround;
    }
    public void setLife(double life) {
        this.life = life;
    }
    public void setGravity(double gravity) {
        this.gravity = gravity;
    } 
    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public void move(Main mainparent) {

        age += 1;

        if ((y > mainparent.getHeight() / 2 && removeOnGround) || age >= life) {
            mainparent.removeParticle(this);
            return;

        }

        if(mainparent.player_1.hitWorm(bounds)==true)
        {

           
           // mainparent.removeParticle(this);
           // mainparent.player_1.hurtWorm(1);
           // return;
        }

        direction = calcXYspeed(Worm.getXspeed(direction, speed), Worm.getYspeed(direction, speed) + gravity);

        x += Worm.getXspeed(direction, speed);
        y += Worm.getYspeed(direction, speed);
    }

    public void draw(Graphics g, Main mainparent) {
        g.setColor(color);
        int move = mainparent.getSize().width / 2;
        move -= mainparent.getScroll();
        g.fillOval(move + (int) (x - radius), (int) (y - radius), (int) (radius), (int) (radius));
    }
}
